package com.monolithiot.sso.common.util;

import java.util.Random;
import java.util.UUID;

/**
 * 2018/11/19 Create by 郭文梁
 * IdentityUtil
 * ID工具类
 *
 * @author 郭文梁
 * @date 2018/11/19
 */
public class IdentityUtil {
    /**
     * 数字字典
     */
    private static final char[] NUMERIC_DICT = "0123456789".toCharArray();
    /**
     * 随机生成器
     */
    public static final Random RANDOM = new Random();

    /**
     * 随机UUID
     *
     * @return UUID as string
     */
    public static String randomUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 生成随机UUID 只包含数字和字母 不带横杠 [-]
     *
     * @return UUID as string
     */
    public static String randomPrettyUUID() {
        return randomUUID().replaceAll("-", "");
    }

    /**
     * 生成随机数据字符串
     *
     * @param length 长度
     * @return 随机字符串
     */
    public static String randomNumericString(int length) {
        char[] chars = new char[length];
        for (int i = 0; i < length; i++) {
            int index = RANDOM.nextInt(NUMERIC_DICT.length);
            chars[i] = NUMERIC_DICT[index];
        }
        return new String(chars);
    }
}
